#include <stdlib.h>

int calculate(char * s){
    int *stack = (int *)malloc(sizeof(int) * (strlen(s) + 1));
    int top = -1;
    int num = 0;
    char sign = '+';
    for (int i = 0; s[i]; i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            num = num * 10 + (s[i] - '0');
        }
        if ((s[i] < '0' && s[i] != ' ') || s[i + 1] == '\0') {
            switch (sign) {
                case '+':
                    stack[++top] = num;
                    break;
                case '-':
                    stack[++top] = -num;
                    break;
                case '*':
                    stack[top] *= num;
                    break;
                case '/':
                    stack[top] /= num;
                    break;
            }
            sign = s[i];
            num = 0;
        }
    }
    int result = 0;
    for (int i = 0; i <= top; i++) {
        result += stack[i];
    }
    free(stack);
    return result;
}
